package com.example.testcalendaractivity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private Button goToCalendar;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        goToCalendar = findViewById(R.id.goToCalendar);
        goToCalendar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED
                        || ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.WRITE_CALENDAR , Manifest.permission.READ_CALENDAR}, 1);
                } else {
                    Intent intent = new Intent(MainActivity.this,TestCalendarActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 用户授予了权限
                Toast.makeText(this, "yes", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this,TestCalendarActivity.class);
                startActivity(intent);
            } else {
                // 用户拒绝了权限，显示提示
                Toast.makeText(this, "永久拒绝了权限，请前往系统设置开启权限", Toast.LENGTH_SHORT).show();
            }
        }
    }


//    // 若当前页面申请了多个权限,处理权限请求结果
//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//            switch (requestCode) {
//                case REQUEST_CALENDAR_PERMISSION:
//                    goToCalendar();
//                    break;
//                case REQUEST_CONTACTS_PERMISSION:
//                    goToContacts();
//                    break;
//                case REQUEST_STORAGE_PERMISSION:
//                    goToStorage();
//                    break;
//            }
//        } else {
//            Toast.makeText(this, "权限被拒绝", Toast.LENGTH_SHORT).show();
//        }
//    }

}